import { clearLocalStorage, getLocalStorage } from '@/utils/local';
import { message } from 'antd';
import { history } from 'umi';
import { BASE_URL1 } from './config';
import MLRequest from './require';

const $api1 = new MLRequest({
  baseURL: BASE_URL1,
  interceptors: {
    requestSuccessFn: (req) => {
      // console.log('$api1的请求成功拦截');
      req.headers.Authorization = 'Bearer ' + getLocalStorage('token');
      return req;
    },
    requestFailureFn: (err) => {
      // console.log('$api1的请求失败拦截');
      return err;
    },
    responeseSuccessFn: (res) => {
      // console.log('$api1的响应成功拦截');
      const { code, message: msg } = res.data;

      if (code !== 200) {
        message.error(msg);
      }
      if (code === 10007) {
        clearLocalStorage();
        history.push('/login');
      }
      return res.data;
    },
    responeseFailureFn: (err) => {
      // console.log('$api1的响应失败拦截');
      return err;
    },
  },
});

const $api2 = new MLRequest({
  baseURL: BASE_URL1,
  interceptors: {
    requestSuccessFn: (req) => {
      console.log('$api2的请求成功拦截');
      return req;
    },
    requestFailureFn: (err) => {
      console.log('$api2的请求失败拦截');
      return err;
    },
    responeseSuccessFn: (res) => {
      console.log('$api2的响应成功拦截');
      return res.data;
    },
    responeseFailureFn: (err) => {
      console.log('$api2的响应失败拦截');
      return err;
    },
  },
});

export { $api1, $api2 };
